//
//  CYTViewController.m
//  PT
//
//  Created by jarze on 15/6/14.
//  Copyright (c) 2015年 jarze. All rights reserved.
//

#import "CYTViewController.h"

#import "BasicTableViewController.h"

#import "CTTableViewCell.h"
#import "MeItem.h"
#import "NextItem.h"

#import "PTStudySAppointmentViewController.h"
#import "MMPickerView.h"
#import "PTTitleButton.h"
#import "MultiSelectPicker.h"
@interface CYTViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *datas;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *stringsArray;
@property (nonatomic, strong) NSString * selectedString;
@property (nonatomic, strong) NSMutableArray *timeArray;

@property (nonatomic, strong) UIButton *timeButton;
@property (nonatomic, strong) UIButton *genderButton;
@property (nonatomic, strong) UIButton *languageButton;



@end

@implementation CYTViewController

- (NSMutableArray *)datas
{
    if (_datas == nil) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    CGFloat h = self.navigationController.navigationBar.frame.size.height;
    CGFloat Width = self.view.frame.size.width;
    CGFloat Height = self.view.frame.size.height;

    self.title = @"Choose Your Tesacher";
    
    [self setupToolBar];
    
    [self addGroup];

    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 2*h +20, Width , Height - 2*h - 20) style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.delegate = self ;
    tableView.dataSource = self;
    

    
}

-(void)setupToolBar
{
    UIView *chooseView = [[UIView alloc]init];
    
    chooseView.frame = CGRectMake(0, 64, 320, 44);
    chooseView.backgroundColor = [UIColor whiteColor];
    
    PTTitleButton *timeButton = [[PTTitleButton alloc]init];
    timeButton.height = 44;
    timeButton.x = 10;
    NSString *time = @"Today 15:30";
    [timeButton setTitle:time forState:UIControlStateNormal];
    [timeButton setImage:[UIImage imageNamed:@"ico_arrow_Down"] forState:UIControlStateNormal];
    [timeButton addTarget:self action:@selector(timeSelect:) forControlEvents:UIControlEventTouchUpInside];
    [chooseView addSubview:timeButton];
    _timeButton = timeButton;
    
    
    PTTitleButton *genderButton = [[PTTitleButton alloc]init];
    genderButton.height = 44;
    genderButton.x = 100;
    NSString *gender = @"Gender";
    [genderButton setTitle:gender forState:UIControlStateNormal];
    [genderButton setImage:[UIImage imageNamed:@"ico_arrow_Down"] forState:UIControlStateNormal];
    [genderButton addTarget:self action:@selector(genderSelect:) forControlEvents:UIControlEventTouchUpInside];
    [chooseView addSubview:genderButton];
    _genderButton = genderButton;
    
    PTTitleButton *languageButton = [[PTTitleButton alloc]init];
    languageButton.height = 44;
    languageButton.x = 200;
    NSString *language = @"Language";
    [languageButton setTitle:language forState:UIControlStateNormal];
    [languageButton setImage:[UIImage imageNamed:@"ico_arrow_Down"] forState:UIControlStateNormal];
    [languageButton addTarget:self action:@selector(languageSelect:) forControlEvents:UIControlEventTouchUpInside];
    [chooseView addSubview:languageButton];
    _languageButton = languageButton;
    
    [self.view addSubview:chooseView];


}
//语言选择栏
- (void)languageSelect:(UIButton *)sender
{
    _stringsArray = @[@"中文",@"English",@"Russian",@"German",@"Spanish",@"Andorra"];
    _selectedString = [_stringsArray objectAtIndex:0];
    
    [MMPickerView showPickerViewInView:self.view withStrings:_stringsArray withOptions:@{
                                                                                         MMtoolbarBackgroundImage: [UIImage imageNamed:@"Mylanguage"],
                                                                                         MMbackgroundColor: [UIColor whiteColor],
                                                                                         MMtoolbarColor: [UIColor clearColor],
                                                                                         MMfont: [UIFont systemFontOfSize:18],
                                                                                         MMvalueY: @3,
                                                                                         MMselectedObject:_selectedString,
                                                                                         MMtextAlignment:@1}
                            completion:^(NSString *selectedString) {
                                _selectedString = selectedString;
                                [_languageButton setTitle:_selectedString forState:UIControlStateNormal];
                            }];
    
}
//性别选择栏
- (void)genderSelect:(UIButton *)sender
{
    _stringsArray = @[@"Either",@"Male",@"Female"];
    _selectedString = [_stringsArray objectAtIndex:0];
    
    [MMPickerView showPickerViewInView:self.view withStrings:_stringsArray withOptions:@{
                                                                                         MMtoolbarBackgroundImage: [UIImage imageNamed:@"gender of my teacher"],
                                                                                         MMbackgroundColor: [UIColor whiteColor],
                                                                                         MMtoolbarColor: [UIColor clearColor],
                                                                                         MMfont: [UIFont systemFontOfSize:18],
                                                                                         MMvalueY: @3,
                                                                                         MMselectedObject:_selectedString,
                                                                                         MMtextAlignment:@1}
                            completion:^(NSString *selectedString) {
                                _selectedString = selectedString;
                                [_genderButton setTitle:_selectedString forState:UIControlStateNormal];

                            }];
}
//时间选择栏
- (void)timeSelect:(UIButton *)sender
{
    NSMutableArray *timeArray = [[NSMutableArray alloc]initWithArray:@[@"Today",@"Tomorrow"]];
    NSDateFormatter *outputFormat = [[NSDateFormatter alloc]init];
    [outputFormat setDateFormat:@"d MMM (EEE)"];
    NSDate *today = [[NSDate alloc]init];
    NSTimeInterval secondsPerDay = (NSTimeInterval)24*60*60 ;
    today = [today dateByAddingTimeInterval:2 * secondsPerDay];
    for (int i = 3; i <= 7; i++) {
        NSString *string = [outputFormat stringFromDate:today];
        [timeArray addObject:string];
        today = [today dateByAddingTimeInterval: secondsPerDay];
    }
    _timeArray = timeArray;
    _selectedString = [_timeArray objectAtIndex:0];
    [MultiSelectPicker showPickerViewInView:self.view withStrings:_timeArray withOptions:@{
                                                                                         MMtoolbarBackgroundImage: [UIImage imageNamed:@"Choose your sdutytime"],
                                                                                         MMbackgroundColor: [UIColor whiteColor],
                                                                                         MMtoolbarColor: [UIColor clearColor],
                                                                                         MMfont: [UIFont systemFontOfSize:18],
                                                                                         MMvalueY: @3,
                                                                                         MMselectedObject:_selectedString,
                                                                                         MMtextAlignment:@1}
                            completion:^(NSArray *selectedString) {
                                NSString *string2 = selectedString[1];
                                    string2 = [string2 substringToIndex:5];
                                NSString *string1 = selectedString[0];
                                if (string1.length >8) {
                                    string1 = [string1 substringToIndex:6];

                                }

                                NSString *selectstring = [NSString stringWithFormat:@"%@ %@",string1,string2];

                                _selectedString = selectstring;
                                [_timeButton setTitle:_selectedString forState:UIControlStateNormal];
                            }];

}






- (void)addGroup
{
    NSMutableArray *items = [[NSMutableArray alloc]init];
    Group *group = [[Group alloc] init];
    group.items = items;
    
    
    MeItem *item0 = [[NextItem alloc] initWithIcon:@"Image1" title:@"Margot Chou" subTitle:@"Score:9.5" destClass:[PTStudySAppointmentViewController class]];
    [group.items addObject:item0];

    
    MeItem *item1 = [[NextItem alloc] initWithIcon:@"Image3" title:@"Alice Zhang" subTitle:@"Score:9.0" destClass:[PTStudySAppointmentViewController class]];
    [group.items addObject:item1];
    
    
    MeItem *item2 = [[NextItem alloc] initWithIcon:@"Image2" title:@"Royal White" subTitle:@"Score:9.0" destClass:[PTStudySAppointmentViewController class]];
    [group.items addObject:item2];
    
    [group.items addObject:item0];
    
    [group.items addObject:item1];
    
    [group.items addObject:item2];
    
    [group.items addObject:item0];
    
    [group.items addObject:item1];
    
    [group.items addObject:item2];
    
    [group.items addObject:item0];
    
    [group.items addObject:item1];
    
    [group.items addObject:item2];
    
    
    [self.datas addObject:group];
    
    

}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 68;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 12;

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{

    return 0.01;

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    Group *g = self.datas[0];
    return g.items.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CTTableViewCell *cell = [CTTableViewCell cellWithTableView:self.tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    Group *g = self.datas[0];
    
    //  从组模型中取出对应行的模型

    cell.meItem = g.items[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 立即取消选中
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 先取出对应组的组模型
    Group *g = self.datas[0];
    //  从组模型中取出对应行的模型
    MeItem *item = g.items[indexPath.section];
//    if ([item isKindOfClass:[NextItem class]]) {
//        // 创建目标控制并且添加到栈中
//        NextItem *Item = (NextItem *)item;
//        UIViewController *vc = [[Item.destVC alloc] init];


//    }
    PTStudySAppointmentViewController *Vc = [[PTStudySAppointmentViewController alloc]init];
    Vc.title = @"Study Appointment";
    Vc.meItem = item;
    [self.navigationController pushViewController:Vc animated:YES];

}


@end
